import Vue from 'vue';
const CryptoJS = require('crypto-js');
const {
	SM4
} = require('gm-crypto');

//加密KEY值(后台提供保持一致)
const strKey = "e6c0c643d7c34e1e8c15006e147f9f77";

//加密
export function Sm4encrypt(data) {
	return SM4.encrypt(data.toString(),strKey, {
		inputEncoding: 'utf8',
		outputEncoding: 'base64'
	}).toString();
}

//sm4解密
export function Sm4Dncrypt(data) {
	return SM4.decrypt(data.toString(),strKey, {
		inputEncoding: 'base64',
		outputEncoding: 'utf8'
	}).toString();
}

/*
加密
*/
//加密
Vue.prototype.Sm4Encrypt = Sm4encrypt;
/*
解密
*/
Vue.prototype.Sm4Dncrypt = Sm4Dncrypt;


//DES加密
export function getAesString(data, key, iv) {
	var key = CryptoJS.enc.Utf8.parse(key);
	var iv = CryptoJS.enc.Utf8.parse(iv);
	var encrypted = CryptoJS.AES.encrypt(data, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.toString(); //返回的是base64格式的密文
}

//DES解密
export function getDAesString(encrypted, key, iv) {
	var key = CryptoJS.enc.Utf8.parse(key);
	var iv = CryptoJS.enc.Utf8.parse(iv);
	var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return decrypted.toString(CryptoJS.enc.Utf8);
}

//加密
export function getAES(data) {
	var key = 'AB9FC38DDB59538B'; //密钥
	var iv = 'U2FsdGVkX19I2BTxNNp1/SMq2PTDQoV8Ik0KDX3wEUcNajXHvWxHSrG8BhEqc40H';
	var encrypted = getAesString(data, key, iv); //密文
	var encrypted1 = CryptoJS.enc.Utf8.parse(encrypted);
	return encrypted;
}

//解密
export function getDAes(data) {
	var key = 'AB9FC38DDB59538B'; //密钥
	var iv = 'U2FsdGVkX19I2BTxNNp1/SMq2PTDQoV8Ik0KDX3wEUcNajXHvWxHSrG8BhEqc40H';
	var decryptedStr = getDAesString(data, key, iv);
	return decryptedStr;
}
